<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>智界论坛</title>
    <link rel="stylesheet" href="css/tech-style.css">
</head>
<body>
<nav class="navbar">
    <div class="main-container">
        <a href="index.jsp" class="navbar-brand">智界论坛</a>
        <ul class="navbar-nav">
            <li><a href="index.jsp">首页</a></li>
            <li><a href="postForm.jsp">发帖</a></li>
            <c:choose>
                <c:when test="${not empty sessionScope.user}">
                    <li class="nav-item">
                        <span class="nav-link text-light">
                            <i class="icon-user"></i> 欢迎, ${sessionScope.user.username}
                        </span>
                    </li>
                    <li><a href="logout">注销</a></li>
                </c:when>
                <c:otherwise>
                    <li><a href="login.jsp">登录</a></li>
                    <li><a href="register.jsp">注册</a></li>
                </c:otherwise>
            </c:choose>
        </ul>
    </div>
</nav>

<main class="main-container" style="padding-top: 2rem; padding-bottom: 2rem;">
    <div class="card">
        <div class="card-header">
            <h3>最新帖子</h3>
        </div>
        <div class="card-body">
            <jsp:useBean id="questionDAO" class="com.example.dao.QuestionDAO" scope="page" />
            <c:set var="posts" value="${questionDAO.getAllQuestions()}" scope="request" />

            <c:choose>
                <c:when test="${not empty posts}">
                    <c:forEach items="${posts}" var="post">
                        <div class="post-item">
                            <div class="post-title">
                                <a href="viewPost?id=${post.id}">${post.title}</a>
                            </div>
                            <div class="post-content">
                                    ${fn:escapeXml(fn:substring(post.content, 0, 100))}...
                            </div>
                            <div class="post-meta">
                                <span><i class="icon-user"></i>${post.author}</span>
                                <span><i class="icon-calendar"></i>${post.createTime}</span>
                                <span><i class="icon-comment"></i>${post.commentCount}条评论</span>
                                <span><i class="icon-eye"></i>${post.viewCount}次浏览</span>
                            </div>
                        </div>
                    </c:forEach>
                </c:when>
                <c:otherwise>
                    <div class="empty-state">
                        <p>暂无帖子，<a href="postForm.jsp">点击发帖</a></p>
                    </div>
                </c:otherwise>
            </c:choose>
        </div>
    </div>
</main>

<footer class="footer">
    <div class="footer-content">
        <p>&copy; 2025 智界论坛</p>
        <p>科技改变未来</p>
    </div>
</footer>
</body>
</html>